#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

//链接：https://www.acwing.com/problem/content/description/3271/
//模拟即可

int main(){
    int r,y,g;
    cin >> r >> y >> g;
    int n;
    cin >> n;
    long long  res = 0;
    for (int i = 1; i <= n; i++){
        long long k,t;
        cin >> k >> t;
        if (!k){
            res += t;
        }else {
            if (k == 1)t = r - t;
            else if (k == 2)t = r + y + g - t;
            else t = r + g - t;
            t += res;
            t %= r + y + g;
            if (t < r)res += r - t;
            else if (t >= r + g)res += r + g + y - t + r;
        }
    }
    cout << res;
    return 0;
}